package com.sservice.web.controller.admin;

import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import com.sservice.common.Constants;
import com.sservice.commonBusiness.e.Comment;
import com.sservice.commonBusiness.service.CommentService;
import com.sservice.commonBusiness.vo.CommentVo;
import com.sservice.model.entity.Info;
import com.sservice.model.entity.UserWx;
import com.sservice.model.vo.InfoVo;
import com.sservice.service.service.IInfoService;
import com.sservice.service.service.IUserService;
import com.sservice.service.service.IUserWxService;
import com.sservice.service.service.wx.WxMsg;
import com.sservice.urp.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/admin/info")
public class InfoController {
//    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IInfoService dataService;
    @Autowired
    private CommentService commentService;

    /**
     * 创建
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public BaseResult<Object> create(Info arg, HttpSession session) {
        UserVo user_ = (UserVo) session.getAttribute(Constants.USER);

        List<String> list = dataService.filterSensitiveWord(arg.getContent());// 是否包含敏感词
        if (list.size() != 0) {
            return new BaseResult(false, list.stream().collect(Collectors.joining(",")), 403);
        }

        if (user_ == null) {
            return new BaseResult(false, "未登录!", 401);
        }
        arg.setAuthor(user_.getUserName());
        arg.setComId(String.valueOf(user_.getComid()));
        int service_res = dataService.insertSelective(arg);
        BaseResult<Object> res = new BaseResult(true, service_res);
        return res;
    }

    @Autowired
    WxMsg wxMsg;
    @Autowired
    IUserWxService iUserWxService;

    /**
     * 增加评论
     *
     * @param arg
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/comment", method = RequestMethod.POST)
    public BaseResult<Object> comment(Comment arg, HttpSession session) {
        UserVo user_ = (UserVo) session.getAttribute(Constants.USER);
        arg.setUserId(user_.getId() + "");
        List<String> list = dataService.filterSensitiveWord(arg.getContent());// 是否包含敏感词
        if (list.size() != 0) {
            return new BaseResult(false, list.stream().collect(Collectors.joining(",")), 403);
        }
        int service_res = commentService.insertSelective(arg);
        /**
         * 在线回复内容说明
         */
        if (service_res == 1) { //  处理回复内容
            Info info = dataService.selectByPrimaryKey(Integer.valueOf(arg.getTargetId()));
            UserWx userWx = iUserWxService.selectByPrimaryKey(Integer.valueOf(info.getAuthorId()));
            if (info.getType1().equals(InfoVo.ask)) {
                wxMsg.applyReplyNoticeToUser(
                        String.valueOf(user_.getComid()),
                        userWx.getOpenid(),
                        "/mineanswerinfo/"+info.getId(),
                        info.getContent(),
                        arg.getContent()
                );
            }else if (info.getType1().equals(InfoVo.lawAsk)) {
                wxMsg.applyReplyNoticeToUser(
                        String.valueOf(user_.getComid()),
                        userWx.getOpenid(),
                        "/lawanswerinfo/"+info.getId(),
                        info.getContent(),
                        arg.getContent()
                );
            }else if (info.getType1().equals(InfoVo.compose)) {
                wxMsg.applyReplyNoticeToUser(
                        String.valueOf(user_.getComid()),
                        userWx.getOpenid(),
                        "/composeinfointo/"+info.getId(),
                        info.getContent(),
                        arg.getContent()
                );
            }else if (info.getType1().equals(InfoVo.sugguest)) {
                wxMsg.applyReplyNoticeToUser(
                        String.valueOf(user_.getComid()),
                        userWx.getOpenid(),
                        "/suggestinfointo/"+info.getId(),
                        info.getContent(),
                        arg.getContent()
                );
            }else if (info.getType1().equals(InfoVo.lawHelp)) {
                wxMsg.applyReplyNoticeToUser(
                        String.valueOf(user_.getComid()),
                        userWx.getOpenid(),
                        "/law-helpinfointo/"+info.getId(),
                        info.getContent(),
                        arg.getContent()
                );
            }


        }
        Info info = new Info();
        CommentVo commentVo = new CommentVo();
        commentVo.setTargetId(arg.getTargetId());
        commentVo.setType1(arg.getType1());
        int count = commentService.countByPage(commentVo);
        info.setFavor(count);// 统计回复数量
        info.setId(Integer.valueOf(commentVo.getTargetId()));
        dataService.updateByPrimaryKeySelective(info);
        BaseResult<Object> res = new BaseResult(true, service_res);
        return res;
    }

    /**
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult<Integer> delete(Info arg) {
        int service_res = dataService.deleteByPrimaryKey(arg.getId());
        BaseResult<Integer> res = new BaseResult<Integer>(true, service_res);

        return res;
    }

    /**
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult<Integer> update(Info arg) {
        System.out.println(arg.getStatus());
        int service_res = dataService.updateByPrimaryKeySelective(arg);
        BaseResult<Integer> res = new BaseResult<Integer>(true, service_res);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public BaseResult<Info> get(Info arg) {
        Info service_res = dataService.selectByPrimaryKey(arg.getId());
        BaseResult<Info> res = new BaseResult<Info>(true, service_res);
        return res;
    }

    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult query(InfoVo arg) {
        List<Info> list = dataService.queryByPage(arg);
        int total = dataService.queryCount(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }

    @Autowired
    private IUserService iUserService;

    @ResponseBody
    @RequestMapping(value = "/queryPlain", method = RequestMethod.GET)
    public ListResult queryPlain(InfoVo arg, HttpServletRequest request) {
        UserVo u = iUserService.getLoginedUser(request);
        if (!iUserService.isShiAdminLevel(u)) {//
            arg.setComId(String.valueOf(u.getComid()));
        }
        List<InfoVo> list = dataService.queryPlainByPage(arg);
        int total = dataService.countPlainByPage(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/queryfdPlain", method = RequestMethod.GET)
    public ListResult queryfdPlainByPage(InfoVo arg, HttpServletRequest request) {
        UserVo u = iUserService.getLoginedUser(request);
        if (!iUserService.isShiAdminLevel(u)) {//
            arg.setComId(String.valueOf(u.getComid()));
        }
        List<InfoVo> list = dataService.queryfdPlainByPage(arg);
        int total = dataService.querydrCount(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }
    @ResponseBody
    @RequestMapping(value = "/queryTpl", method = RequestMethod.GET)
    public ListResult queryTpl(InfoVo arg, HttpServletRequest request) {
        UserVo u = iUserService.getLoginedUser(request);
        if (!iUserService.isShiAdminLevel(u)) {//
            arg.setComId(String.valueOf(u.getComid()));
        }
        List<InfoVo> list = dataService.queryTplByPage(arg);
        int total = dataService.countTplByPage(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/queryWithCategoryPlain", method = RequestMethod.GET)
    public ListResult queryWithCategoryPlain(InfoVo arg, HttpServletRequest request) {
        UserVo u = iUserService.getLoginedUser(request);
        if (!iUserService.isShiAdminLevel(u)) {//
            arg.setComId(String.valueOf(u.getComid()));
        }
        List<InfoVo> list = dataService.queryWithCategoryByPage(arg);
        int total = dataService.countWithCategoryByPage(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }

    /**
     * 查询工作台账
     *
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryRecordPlain", method = RequestMethod.GET)
    public ListResult queryRecordPlain(InfoVo arg) {
        List<CommentVo> list = dataService.queryUserRecordByPage(arg);
        int total = dataService.countUserRecordByPage(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }
}
